/*!
 * New Campaign Label.
 * Date: 2015/07/06.
 * Copyright© 2014-2015 Vamaker All Rights Reserved.
 */
require([
    'jquery',
		'pageInit',
		'formValidator',
    'formAjax',
    'notification'
	], function ($, vamakerPage, formValidator, formAjax, VamOpMsg) {
  $(function () {
    var 
      $dataFrm = $('#data-frm'),
      $submitBtn = $dataFrm.find('button[type="submit"]'),
      $operateBtn = $submitBtn.filter('.save-btn'),
      $operateType = $dataFrm.find('.operate-type'),
      dataFieldRule, options, dataValidator;

    $submitBtn.on('click', function (evt) {
      var $me = $(this),
        operate = $me.data('operate'),
        operateTxt;
      operateTxt = '.' + operate + '-btn';
      $operateBtn = $submitBtn.filter(operateTxt);
      $operateType.val(operate);
    });
    // console.log($dataFrm);
    dataFieldRule = {
      campaign_label_name:{
        rule:{
            required:{
                message:VAM.j('Campaign label cannot be empty')
            }
        },
        messageContainer:'campaign_label_name_err'
      }
      // ,
      // campaign_label_comment: {
      //  rule: {
      //    required: {
      //      message: '时间范围不能为空'
      //    }
      //  },
      //  messageContainer: 'campaign_label_comment_err'
      // }
    };
    options = {
      submitOnSucceed: false,
      onvalidate: function(evt) {
        if (evt.resultList.length <= 0) {
          $dataFrm.ajaxSubmit({
            beforeSubmit: function() {
              $operateBtn.button('loading');
            },
            success: function (data) {
              var next = $operateBtn.data('operate');
              $operateBtn.button('reset');
              if (data.status === 'success') {
                  VamOpMsg.messager(data);
                  if (next === 'save-next') {
                    window.location.href = VAM.getVar('NEXT_URL') + '?label_id=' + data.data.id;
                  } else {
                    window.location.href = 'label-list';
                  };
              } else {
                  VamOpMsg.messager(data);
              }
            }
          });
        };
      }
    };
    formValidator.validate($dataFrm, dataFieldRule, options);
  });
});
